package com.geticsen.leetcode;

public class Solution_200 {
    public int numIslands(char[][] grid) {
        int res = 0;
        int rows = grid.length;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == 'V') continue;
                if (grid[i][j] == '1') {
                    res++;
                    mark(grid, i, j);
                }
            }
        }
        return res;
    }

    public void mark(char[][] grid, int row, int col) {
        if (row < 0 || col < 0 || row >= grid.length || col >= grid[row].length) {
            return;
        }
        if (grid[row][col] == '1') {
            grid[row][col] = 'V';
            mark(grid, row + 1, col);
            mark(grid, row, col + 1);
            mark(grid, row, col - 1);
        }

    }
}
